package com.lihua.newideas.config;

import com.lihua.newideas.handler.AdminInterceptorHandler;
import com.lihua.newideas.handler.IndexInterceptorHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//配置一个拦截器

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /*主页处理器
    * */
    @Autowired
    private IndexInterceptorHandler indexInterceptorHandler;

    /*管理员拦截器
    * */
    @Autowired
    private AdminInterceptorHandler adminInterceptorHandler;

/*当访问静态资源（图片）时
 * 将http://localhost:8080/upload/2021/6/fields20210618104946523_small.png 请求拦截下来
 * 换成其他请求路径
 * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 通过addResourceHandler添加资源映射路径，然后通过addResourceLocations来指定路径。可以访问自定义upload文件夹
        registry.addResourceHandler("/upload/admin/**").addResourceLocations("classpath:/upload/admin/")
                .addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/newIdeasFile/upload/admin/");
        registry.addResourceHandler("/upload/user/**").addResourceLocations("classpath:/upload/user/")
                .addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/newIdeasFile/upload/user/");

        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    /*自定义处理器的拦截器
    * 主页拦截器,获取用户个人信息
    * 多个拦截器组成一个拦截器链
     addPathPatterns 用于添加拦截规则
     excludePathPatterns 用户排除拦截
    * */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(indexInterceptorHandler).addPathPatterns("/");
        registry.addInterceptor(adminInterceptorHandler).addPathPatterns("/admin");
    }
}
